var mogu = {
  height:null,
  width:null,
  curPageId:null,
  needCheckLeave:true,
  serverBase:'http://gumo.me/',
  tcp:{
    handle:null,
    id:null,
    init:function(){
      mogu.tcp.handle = io.connect(mogu.serverBase);
      mogu.tcp.handle.on('connected',function(data){
        mogu.tcp.id = data.id;
        mogu.tcp.handle.on('loginSuccess',function(data){
          mogu.user.exist = true;
          for(var key in data.user){
            mogu.user[key] = data.user[key]
          }
          mogu.page.account.userInit();
        });
        mogu.tcp.handle.on('loginError',function(data){
          alert('登录失败:'+data.reason);
          jQuery('#loginAccountInput').focus();
        });
        mogu.tcp.handle.on('regSuccess',function(data){
          mogu.user.exist = true;
          for(var key in data.user){
            mogu.user[key] = data.user[key]
          }
          mogu.page.account.userInit();
        });
        mogu.tcp.handle.on('regError',function(data){
          alert('注册失败:'+data.reason);
          jQuery('#regAccountInput').focus();
        });
      });
    }
  },
  user:{
    exist:false,
    account:null,
    password:null,
    sign:null,
    nickname:null,
    headIndex:null,
    photos:[],
    friendsAccount:[]
  },
  page:{
    desktop:{
      id:'desktop',
      name:'desktopPage',
      pageNavGoal:['accountPage','musicPage','wiziqiPage','aboutPage'],
      pageNavIcon:['emesene','solfege','package_games_strategy','aptana'],
      pageNavSizeRange:[128,72,48,32,24,16],
      pageNavLevel:null,
      pageNavSize:null,
      inited:false,
      init:function(){
        this.inited = true;
        this.pageNavLevel = this.pageNavSizeRange.length-1;
      },
      resize:function(){
        var size = Math.min(150,Math.floor(mogu.width/3));
        var levelChange=false;
        jQuery('.pageNav').css('width',size+'px');
        for(var i = 0; i < this.pageNavSizeRange.length; i++){
          if(size > this.pageNavSizeRange[i]){
            if(i != this.pageNavLevel){
              levelChange=true;
            }
            this.pageNavLevel = i;
            break;
          }
        }
        this.pageNavSize = this.pageNavSizeRange[this.pageNavLevel];
        jQuery('.pageNav img').css('margin-top',size-this.pageNavSize+'px');
        jQuery('.pageNav img').css('margin-left',(size-this.pageNavSize)/2+'px');
        if(!levelChange){return;}
        for(var i = 0; i < this.pageNavGoal.length; i++){
          jQuery('#pageNav'+i+' img').attr('src',
            '/images/icons/'+this.pageNavSize+'x'+this.pageNavSize+'/apps/'+this.pageNavIcon[i]+'.png');
        }
      },
      show:function(){
        if(!this.inited){
          this.init();
        }
        if(mogu.curPageId){
          mogu.page[mogu.curPageId].hide();
        }
        jQuery('#'+this.name).css('display','block');
        mogu.curPageId = this.id;
      },
      hide:function(){
        jQuery('#'+this.name).css('display','none');
      },
      autoChangeBg:function(){
        this.curBgIndex = (this.curBgIndex+1)%this.bgTotal;
        jQuery('#desktopBg').css('background','url(/images/mogu/'+this.curBgIndex+'.jpg) center no-repeat');
        jQuery('#desktopBg').fadeIn(2500,function(){
          jQuery('#desktopBg').fadeOut(2500,function(){
            desktop.autoChangeBg();
          })
        })
      },
      back:function(){

      }
    },
    account:{
      id:'account',
      name:'accountPage',
      inited:false,
      init:function(){
        this.inited = true;
        this.showLoginForm();
      },
      resize:function(){

      },
      userInit:function(){
        jQuery('#'+this.name+' .pageTitle span').html('用户');
        jQuery('#'+this.name+' .pageBackButton img').unbind();
        jQuery('#'+this.name+' .pageBackButton img').bind('click',function(){
          mogu.page.account.back();
        });
        jQuery('#loginForm').css('display','none');
        jQuery('#findBackPasswordForm').css('display','none');
        jQuery('#regForm').css('display','none');
        jQuery('#userInfo').css('display','block');
      },
      doLogin:function(){
        var loginAccount = jQuery('#loginAccountInput').val();
        if(loginAccount == ''){
          alert('请输入账号');
          jQuery('#loginAccountInput').focus();
          return;
        }
        var loginPassword = jQuery('#loginPasswordInput').val();
        if(loginPassword == ''){
          alert('请输入密码');
          jQuery('#loginPasswordInput').focus();
          return;
        }
        mogu.tcp.handle.emit('doLogin',{
          'account':loginAccount,
          'password':loginPassword
        })
      },
      doReg:function(){
        var regAccount = jQuery('#regAccountInput').val();
        if(regAccount == ''){
          alert('请输入账号');
          jQuery('#regAccountInput').focus();
          return;
        }
        var regPassword = jQuery('#regPasswordInput').val();
        if(regPassword == ''){
          alert('请输入密码');
          jQuery('#regPasswordInput').focus();
          return;
        }
        var regPasswordRepeat = jQuery('#regPasswordRepeatInput').val();
        if(regPasswordRepeat != regPassword){
          alert('请输入相同的密码');
          jQuery('#regPasswordRepeatInput').focus();
          return;
        }
        mogu.tcp.handle.emit('doReg',{
          'account':regAccount,
          'password':regPassword
        })
      },
      showLoginForm:function(){
        jQuery('#'+this.name+' .pageTitle span').html('登录');
        jQuery('#'+this.name+' .pageBackButton img').unbind();
        jQuery('#'+this.name+' .pageBackButton img').bind('click',function(){
          mogu.page.account.back();
        });
        jQuery('#loginForm').css('display','block');
        jQuery('#findBackPasswordForm').css('display','none');
        jQuery('#regForm').css('display','none');
      },
      showRegForm:function(){
        jQuery('#'+this.name+' .pageTitle span').html('注册');
        jQuery('#'+this.name+' .pageBackButton img').unbind();
        jQuery('#'+this.name+' .pageBackButton img').bind('click',function(){
          mogu.page.account.showLoginForm();
        });
        jQuery('#loginForm').css('display','none');
        jQuery('#findBackPasswordForm').css('display','none');
        jQuery('#regForm').css('display','block');
      },
      findBackPassword:function(){
        jQuery('#'+this.name+' .pageTitle span').html('找回密码');
        jQuery('#'+this.name+' .pageBackButton img').unbind();
        jQuery('#'+this.name+' .pageBackButton img').bind('click',function(){
          mogu.page.account.showLoginForm();
        });
        jQuery('#loginForm').css('display','none');
        jQuery('#findBackPasswordForm').css('display','block');
        jQuery('#regForm').css('display','none');
      },
      show:function(){
        if(!this.inited){
          this.init();
        }
        if(mogu.curPageId){
          mogu.page[mogu.curPageId].hide();
        }
        jQuery('#'+this.name).css('display','block');
        mogu.curPageId = this.id;
      },
      hide:function(){
        jQuery('#'+this.name).css('display','none');
      },
      back:function(){
        mogu.page.desktop.show();
      }
    },
    music:{
      id:'music',
      name:'musicPage',
      audio:null,
      singerList:['元若蓝','张玉华','SHE','陈绮贞','阿桑','梁静茹','陈绮贞','陈绮贞','自然卷'],
      musicList:['半情歌','原谅','沿海公路的出口','鱼','一直很安静','夜夜夜夜','旅行的意义','还是会寂寞','蘑菇之歌'],
      musicIndex:-1,
      paused:false,
      inited:false,
      init:function(){
        this.inited = true;
        this.audio = document.getElementById('moguMusic');
        var musicListHtml = '';
        for(var i = 0; i < this.musicList.length; i++){
          musicListHtml += '<div class="musicItem" id="musicItem'+i+'" onclick="mogu.page.music.playSelect('+i+')">'
          +'<span class="musicNum">'+(i+1)+'</span><span class="musicName">'+this.musicList[i]+'</span><span class="singerName">--'+this.singerList[i]+'</span></div>';
        }
        jQuery('#musicList').html(musicListHtml);
      },
      resize:function(){
        var listHeight = mogu.height - 100;
        jQuery('#musicList').css('height',listHeight+'px');
      },
      show:function(){
        if(!this.inited){
          this.init();
        }
        if(mogu.curPageId){
          mogu.page[mogu.curPageId].hide();
        }
        jQuery('#'+this.name).css('display','block');
        mogu.curPageId = this.id;
      },
      hide:function(){
        jQuery('#'+this.name).css('display','none');
      },
      playSelect:function(n){
        jQuery('#musicItem'+this.musicIndex).css('background-color','#eee');
        jQuery('#musicItem'+this.musicIndex).css('color','#000');
        this.musicIndex = n;
        this.play();
      },
      playNext:function(){
        var select = (this.musicIndex+1)%this.musicList.length;
        this.playSelect(select);
      },
      playLast:function(){
        var select = this.musicIndex-1;
        if(select < 0){
          select += this.musicList.length;
        }
        this.playSelect(select);
      },
      play:function(){
        jQuery('#playMusicButton').attr('src','/images/icons/48x48/actions/media-playback-pause.png');
        jQuery('#musicItem'+this.musicIndex).css('background-color','#666');
        jQuery('#musicItem'+this.musicIndex).css('color','#fff');
        this.audio.src = "/media/"+this.musicList[this.musicIndex]+'.mp3';
        this.audio.play();
      },
      pause:function(){
        jQuery('#playMusicButton').attr('src','/images/icons/48x48/actions/media-playback-start.png');
        this.audio.pause();
      },
      start:function(){
        jQuery('#playMusicButton').attr('src','/images/icons/48x48/actions/media-playback-pause.png');
        this.audio.play();
      },
      playOrPause:function(){
        if(this.musicIndex < 0){return;}
        if(this.paused){
          this.start();
          this.paused = false;
        }
        else{
          this.pause();
          this.paused = true;
        }
      },
      selectPlayMode:function(){
        /*bgMusic.addEventListener('ended', function (){
          this.musicIndex = (this.musicIndex+1)%this.musicList.length;
          bgMusic.src = "/media/"+this.musicList[this.musicIndex]+'.mp3';
          setTimeout(function (){
            bgMusic.play();
          },500);
        },false);*/
      },
      back:function(){
        mogu.page.desktop.show();
      }
    },
    wuziqi:{
      id:'wuziqi',
      name:'wuziqiPage',
      inited:false,
      init:function(){
        this.inited = true;
      },
      resize:function(){

      },
      show:function(){
        if(!this.inited){
          this.init();
        }
        if(mogu.curPageId){
          mogu.page[mogu.curPageId].hide();
        }
        jQuery('#'+this.name).css('display','block');
        mogu.curPageId = this.id;
      },
      hide:function(){
        jQuery('#'+this.name).css('display','none');
      },
      back:function(){
        mogu.page.desktop.show();
      }
    },
    about:{
      id:'about',
      name:'aboutPage',
      inited:false,
      init:function(){
        this.inited = true;
      },
      resize:function(){

      },
      show:function(){
        if(!this.inited){
          this.init();
        }
        if(mogu.curPageId){
          mogu.page[mogu.curPageId].hide();
        }
        jQuery('#'+this.name).css('display','block');
        mogu.curPageId = this.id;
      },
      hide:function(){
        jQuery('#'+this.name).css('display','none');
      },
      back:function(){
        mogu.page.desktop.show();
      }
    }
  },
  resize:function(){
    if (window.innerHeight){
      this.height = window.innerHeight;
      this.width = window.innerWidth;
    }
    else if(document.body&&document.body.clientHeight){
      this.height = document.body.clientHeight;
      this.width = document.body.clientWidth;
    }
    if (document.documentElement&&document.documentElement.clientHeight&&document.documentElement.clientWidth){
      this.height = document.documentElement.clientHeight;
      this.width = document.documentElement.clientWidth;
    }
    jQuery('body').css('height',this.height+'px');
    for(var key in this.page){
      this.page[key].resize();
    }
  },
  checkLeave:function(){
    if(this.needCheckLeave){
      return '真的要走么?';
    }
  }
};
function init(){
  mogu.resize();
  mogu.tcp.init();
  mogu.page.desktop.show();
}
jQuery('body').ready(init);